package JDBC;

import java.sql.*;
import java.util.ResourceBundle;

public class DBUtil {
    private static ResourceBundle bundle = ResourceBundle.getBundle("resources/db");

    /**
     * 获取数据库连接
     * @return
     * @throws Exception
     */
    public static Connection getConnection() {
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");
        Connection con = null;
        try {
            con = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    /**
     * 关闭资源
     * @param con
     * @param stmt
     * @param rs
     */
    public static void close(Connection con, Statement stmt, ResultSet rs){

        if(rs != null){
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        if(stmt != null){
            try {
                stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        if(con != null){
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public static void close(Connection con, Statement stmt){
        if(stmt != null){
            try {
                stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        if(con != null){
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
